package com.guinea.generator;

import java.util.Iterator;

import com.guinea.core.utils.EncodeUtils;

public class ConfigUtils {
	public static final String TYPE_STRING = "String";

	public static final String TYPE_LONG = "Long";

	public static final String TYPE_DATE = "Date";

	public static final String TYPE_DOUBLE = "Double";

	public static String camelToTable(String src) {
		String result = "";
		for (int i = 0; i < src.length(); i++) {
			char c = src.charAt(i);
			if (Character.isUpperCase(c) && i > 0)
				result += "_" + c;
			else
				result += c;
		}
		return result.toUpperCase();
	}

	public static String camelToTable(String src, String prefix) {
		String result = (prefix == null ? "" : prefix + "_")
				+ ConfigUtils.camelToTable(src);
		return result.toUpperCase();
	}

	public static String decodeUnicode(final String dataStr) {
		return EncodeUtils.gbEncoding(dataStr);
	}

	public static MappingClass findClass(String name, Config config) {
		MappingClass c = null;
		for (Iterator iter = config.getClasses().iterator(); iter.hasNext();) {
			MappingClass mc = (MappingClass) iter.next();
			if (mc.getName().equals(name)) {
				c = mc;
				break;
			}
		}
		if(c == null) System.out.println("Error: cannot find class " + name);
		return c;
	}
}
